<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\ding;

use app\common\model\ding\ClerkMember as MemberModel;
use app\common\service\store\User as UserService;
use think\facade\Db;

/**
 * 文章模型
 * Class Member
 * @package app\store\model
 */
class Member extends MemberModel
{
    /**
     * 获取列表
     * @param array $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param = [])
    {
        // 查询参数
        $params = $this->setQueryDefaultValue($param, [
            'search' => '', // 文章标题
            'shopId' => 0,  // 文章分类id
            'status' => -1, // 文章状态
        ]);
        // 检索查询条件
        $filter = [];
        // 文章标题
        ! empty($params['search']) && $filter[] = ['mobile|real_name', 'like', "%{$params['search']}%"];

        // 当前用户信息

        $store_user_id = UserService::getLoginInfo()['user']['store_user_id'];

        $shopId = Db::name('store_user')->where('store_user_id', $store_user_id)->value('shop_id');
        if ($shopId > 0) {
            $filter[] = ['shop_id', '=', $shopId];
        } else {
            // 文章分类id
            $params['shopId'] > 0 && $filter[] = ['shop_id', '=', $params['shopId']];
        }

        $filter[] = ['clerk_id', '=', 0];
        // 查询列表数据
        return $this

            ->where($filter)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate(15);
    }
    public function getListAll(array $param = [])
    {
        // 查询参数
        $params = $this->setQueryDefaultValue($param, [
            'search' => '', // 文章标题
            'shopId' => 0,  // 文章分类id
            'status' => -1, // 文章状态
            'day'    => 0,  //多久未到店的客户数据,0全部
        ]);
        // 检索查询条件
        $filter = [];
        // 文章标题
        ! empty($params['search']) && $filter[] = ['mobile|real_name', 'like', "%{$params['search']}%"];
        // 当前用户信息

        $store_user_id = UserService::getLoginInfo()['user']['store_user_id'];

        $shopId = Db::name('store_user')->where('store_user_id', $store_user_id)->value('shop_id');
        if ($shopId > 0) {
            $filter[] = ['shop_id', '=', $shopId];
        } else {
            // 文章分类id
            $params['shopId'] > 0 && $filter[] = ['shop_id', '=', $params['shopId']];
        }
        if ($params['day'] > 0) {
            $time=time()-$params['day']*86400;
            $filter[] = ['last_time', '<=', $time];
        }
        $filter[] = ['clerk_id', '=', 0];
        // 查询列表数据
        return $this

            ->where($filter)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->select()->toArray();
    }

    /**
     * 新增记录
     * @param array $data
     * @return false|int
     */
    public function add(array $data)
    {

        if (empty($data['content'])) {
            $this->error = '请输入文章内容';
            return false;
        }
        $data['store_id'] = self::$storeId;
        return $this->save($data);
    }

    /**
     * 更新记录
     * @param array $data
     * @return bool|int
     */
    public function edit(array $data)
    {

        if (empty($data['content'])) {
            $this->error = '请输入文章内容';
            return false;
        }
        return $this->save($data) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 获取文章总数量
     * @param array $where
     * @return int|string
     */
    public static function getMemberTotal(array $where = [])
    {
        return (new static )->where($where)->where('is_delete', '=', 0)->count();
    }

}
